package algorithm;

/**
 * 位运算实现加减乘除
 */
public class LeetcodeTest1 {


    public static int add(int a, int b) {
        if (a == 0) {
            return b;
        }
        if (b == 0) {
            return a;
        }
        int sum;
        while (b != 0) {
            sum = a^b;
            b = (a&b)<<1;
            a = sum;
        }
        return a^b;
    }

    /**
     * 减去一个数，等同于加一个负数  a-b==>a+(-b)
     * @param a
     * @param b   取反加1
     * @return
     */
    public static int minus(int a, int b) {
        b= ~b + 1;
        return add(a, b);
    }

    public static void main(String[] args) {
        System.out.println(minus(10, 2));
        System.out.println(minus(0, 2));
        System.out.println(minus(2, 0));
        System.out.println(minus(2, 10));
    }
}
